﻿namespace ManagerClinic.DanhMuc
{
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using ManagerClinic.Modules;
    using System.Linq;
    using ManagerClinic.Core.Business;
    using ManagerClinic.Core.Common;
    using ManagerClinic.Ultilities.Ultility;

    public partial class frmDmKho : DevExpress.XtraEditors.XtraForm
    {
        GridCheckMarksSelection gridCheckMarksSelection = null;
        DmKhoaBusiness dmKhoaBusiness = new DmKhoaBusiness();
        IList<DmKhoEntity> lstDmKhoEntity = null;
        DmKhoBusiness dmKhoBusiness = new DmKhoBusiness();

        public frmDmKho()
        {
            InitializeComponent();
            gridCheckMarksSelection = new GridCheckMarksSelection(gvDmKho);
            LoadDMKhoa();
        }

        void LoadDMKhoa()
        {
            var lstDmKhoEntityaEntity = dmKhoaBusiness.GetDmKhoaList();
            var dmKhoaEntity = new DmKhoaEntity()
            {
                MaKhoa = ConstantManagerClinic.sValueMember,
                TenKhoa = ConstantManagerClinic.sDisplayMember
            };
            lstDmKhoEntityaEntity.Insert(0, dmKhoaEntity);

            lkDMKhoa.Properties.DataSource = lstDmKhoEntityaEntity;
            lkDMKhoa.Properties.DisplayMember = "TenKhoa";
            lkDMKhoa.Properties.ValueMember = "MaKhoa";
            lkDMKhoa.EditValue = ConstantManagerClinic.sValueMember;
        }

        void LoadData(object sender, EventArgs e)
        {
            lstDmKhoEntity = dmKhoBusiness.GetDmKhoList();

            if (lkDMKhoa.EditValue.ToString() != ConstantManagerClinic.sValueMember)
                lstDmKhoEntity = lstDmKhoEntity.Where(n => n.MaKhoa == lkDMKhoa.EditValue.ToString()).ToList();

            grdDmKho.DataSource = lstDmKhoEntity;
        }

        private void frmDMKho_Load(object sender, EventArgs e)
        {
            Authenticate.IsAuthenticateForm(this);
            LoadData(sender, e);
        }

        private void lkDMKhoa_EditValueChanged(object sender, EventArgs e)
        {
            LoadData(sender, e);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            var deleteObjects = gridCheckMarksSelection.selection;

            if (deleteObjects.Count <= 0)
            {
                MessageBox.Show("Bạn chưa chọn bản ghi để xóa!", ConstantManagerClinic.sTitleInformation, MessageBoxButtons.OK);
                return;
            }

            if (MessageBox.Show("Bạn chắc chắn muốn xóa dữ liệu?", ConstantManagerClinic.sTitleWarming, MessageBoxButtons.YesNo) == DialogResult.No)
            {
                gridCheckMarksSelection.ClearSelection();
                return;
            }
            foreach (DmKhoEntity item in deleteObjects)
            {
                dmKhoBusiness.DeleteDmKho(item.MaKho);
            }
            gridCheckMarksSelection.ClearSelection();
            LoadData(sender, e);
        }

        DmKhoEntity GetRow()
        {
            if (gvDmKho.RowCount > 0)
            {
                return (DmKhoEntity)gvDmKho.GetFocusedRow();
            }
            return null;
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            var dmKhoEntity = GetRow();
            if (dmKhoEntity == null) return;
            var frm = new frmCapNhatDmKho((int)ConstantManagerClinic.StateInsertOrUpdate.Update, dmKhoEntity);
            frm.dmKhoaEvent += new EventHandler(LoadData);
            frm.ShowDialog();

        }

        private void gvDMKho_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
                e.Handled = false;
            }
        }

        private void grdDMKho_DoubleClick(object sender, EventArgs e)
        {
            if (btnSua.Enabled == false) return;
            btnSua_Click(sender, e);
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnThemMoi_Click(object sender, EventArgs e)
        {
            var frm = new frmCapNhatDmKho((int)ConstantManagerClinic.StateInsertOrUpdate.Insert);
            frm.dmKhoaEvent += new EventHandler(LoadData);
            frm.ShowDialog();
        }
    }
}